package metu.tespit.session.schedule;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import metu.tespit.entitiy.accountManagement.User;
import metu.tespit.entity.course.Activity;
import metu.tespit.entity.course.ProjectDocument;
import metu.tespit.entity.course.ProjectTeam;
import metu.tespit.entity.course.ReviewPair;
import metu.tespit.session.EntityManager.ExtendedEntityHome;
import metu.tespit.session.accountManagement.UserHome;
import metu.tespit.session.course.CourseHome;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.security.Identity;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

/**
 
 * @created May 28, 2012
 */
@Name("activityHome")
public class ActivityHome extends ExtendedEntityHome<Activity>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6549846541321L;

	@In(create=true)
	private CourseHome courseHome;
	
	@RequestParameter
	protected Long activityId;
	
	private ProjectDocument uploaded;
	
	@In
	EntityManager entityManager;
	
//	@In
//	UserHome userHome;
	
	
	@Override
	public Object getId()
	{
		if (activityId == null)
		{
			return super.getId();
		}
		else
		{
			return activityId;
		}
	}

	private boolean submissionActivity = false;
	private boolean reviewActivity = false;

	private ProjectTeam reviewer;

	private ProjectTeam reviewee;

	private List<ReviewPair> reviewPairs = new ArrayList<ReviewPair>();


	public Activity getInstance(){

		super.getInstance();

		if(this.instance.getId()==null){

			return instance;

		}
		else{
			this.setReviewPairs(instance.getReviewPairs());
			this.setSubmissionActivity(instance.getSubmissionActivity());
			this.setReviewActivity(instance.getReviewActivity()!=null);
		}
		return this.instance;
	}

    @Transactional
	public String persistWithCourse(){
		// TODO: adam gibi persist
		if(courseHome!=null){

			this.instance.setSubmissionActivity(submissionActivity);
			this.instance.setSchedule(courseHome.getInstance().getSchedule());

			this.instance.setReviewPairs(reviewPairs);

			return super.persist();
		}else{

			return "FAIL";

		}

	}
    
	public void listener(UploadEvent event) throws Exception{
		uploaded = new ProjectDocument();
		UploadItem item = event.getUploadItem();
		uploaded.setContentType(item.getContentType());
		File file =item.getFile();
		RandomAccessFile f = new RandomAccessFile(file, "r");
		byte[] b = new byte[(int)f.length()];
		f.read(b);
		uploaded.setData(b);
		uploaded.setFileName(item.getFileName());

	}  


	@In
	Identity identity;
	
    public String upload(){
    	if(this.instance.getDueDate().before(new Date())){
    		FacesMessages.instance().add("Due Date is passed, you cannot update");
    		return "fault";
    	}
    	User us=(User)entityManager.createQuery("Select u from User u where u.username=:un").setParameter("un", identity.getCredentials().getUsername()).getSingleResult();
    	uploaded.setUploader(us);
    	uploaded.setProjectTeam(us.getProjectTeam());
    	uploaded.setUploaderActivity(this.instance);
    	entityManager.persist(uploaded);
    	this.instance.getProjectDocuments().add(uploaded);
    	
    	String retval = super.update();
    	System.out.println(retval);
    	uploaded=null;
    	return retval;
    	
    }
    
    public void clearUploaded(){
    	uploaded=null;
    }
    
	public void addPair(){

		ReviewPair p = new ReviewPair();
		p.setReviewee(reviewee);
		p.setReviewer(reviewer);
		reviewPairs.add(p);
		reviewee=null;
		reviewer=null;



	}
	public void removePair(ReviewPair p){

		reviewPairs.remove(p);

	}
	public void clearInstance(){
		submissionActivity = false;
		setReviewActivity(false);
		reviewer=null;
		reviewee=null;
		reviewPairs = new ArrayList<ReviewPair>();
		super.clearInstance();
	}
	
	public boolean dateCheck(){
		
		return this.instance.getDueDate().after(new Date());
	}

	public boolean isSubmissionActivity() {
		return submissionActivity;
	}

	public void setSubmissionActivity(boolean submissionActivity) {
		this.submissionActivity = submissionActivity;
		if(!submissionActivity)
			this.reviewActivity=false;
	}

	public void setReviewActivity(boolean reviewActivity) {
		this.reviewActivity = reviewActivity;
	}

	public boolean isReviewActivity() {
		return reviewActivity;
	}


	public List<ReviewPair> getReviewPairs() {
		return reviewPairs;
	}

	public void setReviewPairs(List<ReviewPair> reviewPairs) {
		this.reviewPairs = reviewPairs;
	}

	public ProjectTeam getReviewer() {
		return reviewer;
	}

	public void setReviewer(ProjectTeam reviewer) {
		this.reviewer = reviewer;
	}

	public ProjectTeam getReviewee() {
		return reviewee;
	}

	public void setReviewee(ProjectTeam reviewee) {
		this.reviewee = reviewee;
	}

    public Long getActivityId() {
        return activityId;
    }

    public void setActivityId(Long activityId) {
        this.activityId = activityId;
    }
}
